package com.sto.stoapp.http.request;

import java.nio.ByteBuffer;

import com.sto.stoapp.http.BaseMessage;
import com.sto.stoapp.http.FieldLength;
import com.sto.stoapp.http.MessageType;

/**
 * 在线查询协议
 * 
 * @author Administrator
 * 
 */
public class OnlineInquriyRequest extends BaseMessage implements FieldLength {
	// 在线查询参数个数
	private final int INQUIRY_PARAM_COUNT = 4;
	private final int CODE_LENGTH = 16;

	// 网点编号
	private String siteNumber;
	// 工号
	private String jobNumber;
	// 单号
	private String code;

	public OnlineInquriyRequest(String siteNumber, String jobNumber, String code) {
		this.siteNumber = siteNumber;
		this.jobNumber = jobNumber;
		this.code = code;
	}

	/**
	 * 在线查询协议打包
	 * 
	 * @return
	 */
	public byte[] toByte() {
		int total = MESSAGE_TYPE_LENGTH + SITE_CODE_LENGTH + JOB_NUMBER_LENGTH
				+ CODE_LENGTH + INQUIRY_PARAM_COUNT;
//		StringBuffer buffer = new StringBuffer();
//
//		buffer.append(plusString("" + total, MESSAGE_BODY_LENGTH) + SEPARATOR);
//
//		buffer.append(plusString("" + MessageType.ONLINE_INQUIRY_TYPE,
//				MESSAGE_TYPE_LENGTH) + SEPARATOR);
//
//		buffer.append(plusString(siteNumber, SITE_CODE_LENGTH) + SEPARATOR);
//
//		buffer.append(plusString(jobNumber, JOB_NUMBER_LENGTH) + SEPARATOR);
//
//		buffer.append(plusString(code, CODE_LENGTH) + SEPARATOR);
//
//		return buffer.toString().getBytes();
		ByteBuffer sb = ByteBuffer.allocate(total + MESSAGE_BODY_LENGTH + 1);
		sb.put(plusString("" + total, MESSAGE_BODY_LENGTH).getBytes()).put(
				SEPARATOR.getBytes());
		sb.put(plusString("" + MessageType.ONLINE_INQUIRY_TYPE,
				MESSAGE_TYPE_LENGTH).getBytes()).put(SEPARATOR.getBytes());
		sb.put(plusString(siteNumber, SITE_CODE_LENGTH).getBytes()).put(
				SEPARATOR.getBytes());
		sb.put(plusString(jobNumber, JOB_NUMBER_LENGTH).getBytes()).put(
				SEPARATOR.getBytes());
		sb.put(plusString(code, CODE_LENGTH).getBytes()).put(
				SEPARATOR.getBytes());
		return sb.array();
	}
}
